package pers.qianyu.month_202011.date_20201125;

/**
 * 516. 最长回文子序列
 * https://leetcode-cn.com/problems/longest-palindromic-subsequence/
 *
 * @author mizzle rain
 * @date 2020-11-25 17:22
 */
public class LongestPalindromeSubseq {
    public int longestPalindromeSubseq(String s) {
        int[][] dp = new int[s.length()][s.length()];
        for (int i = 0; i < s.length(); i++) {
            dp[i][i] = 1;
        }
        for (int i = s.length() - 2; i >= 0; i--) {
            for (int k = i + 1; k < s.length(); k++) {
                if (s.charAt(i) == s.charAt(k)) {
                    dp[i][k] = dp[i + 1][k - 1] + 2;
                } else {
                    dp[i][k] = Math.max(dp[i][k - 1], dp[i + 1][k]);
                }
            }
        }
        return dp[0][s.length() - 1];
    }

    public static void main(String[] args) {
        int result = new LongestPalindromeSubseq().longestPalindromeSubseq("bbbab");
        System.out.println("result = " + result);
    }
}
